package lion.framework.core.web;

import javax.servlet.http.HttpSession;

/**
 * @author lion
 * @date 2017年8月14日
 */
public class SessionContext {

	private HttpSession session;

	public static ThreadLocal<SessionContext> context = new ThreadLocal<>();

	public SessionContext(HttpSession session) {
		this.session = session;
	}

	public static void setSession(HttpSession session) {
		context.set(new SessionContext(session));
	}

	public static void unset() {
		context.set(null);
	}

	@SuppressWarnings("unchecked")
	public static <T> T get(String key) {

		SessionContext sessionContext = context.get();
		if (sessionContext == null) { return null; }
		return (T) sessionContext.session.getAttribute(key);
	}

	public static void put(String key, Object value) {
		SessionContext sessionContext = context.get();
		if (sessionContext == null) { return; }
		sessionContext.session.setAttribute(key, value);
	}

}
